package com.hjm.project.team.teamYlInfo.controller;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.service.IAgentInfoService;
import com.hjm.project.team.policy.domain.TeamPolicy;
import com.hjm.project.team.policy.service.ITeamPolicyService;
import com.hjm.project.team.teamYl.domain.TeamYl;
import com.hjm.project.team.teamYl.service.ITeamYlService;
import com.hjm.project.team.teamYlInfo.domain.TeamYlInfoDo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.team.teamYlInfo.domain.TeamYlInfo;
import com.hjm.project.team.teamYlInfo.service.ITeamYlInfoService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 团员信息Controller
 * 
 * @author maker-platform
 * @date 2021-04-10
 */
@Controller
@RequestMapping("/team/teamYlInfo")
public class TeamYlInfoController extends BaseController
{
    private String prefix = "team/teamYlInfo";
    private static final int MAXPOLICYCOUNT = 4;  //现有最大政策

    @Resource
    private ITeamYlInfoService teamYlInfoService;  //团员信息
    @Resource
    private ITeamYlService teamYlService;           //团队信息
    @Resource
    private IAgentInfoService agentInfoService;     //代理信息
    @Resource
    private ITeamPolicyService teamPolicyService;   //团政策信息

    /**
     *前往团长页面
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:teamYlInfo:view")
    @GetMapping()
    public String teamYlInfo()
    {
        return prefix + "/teamYlInfo";
    }


    /**
     * 前往团员页面
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("team:teamYlStaffInfo:view")
    @GetMapping("/teamYlStaffInfo")
    public String teamYlStaffInfo()
    {
        return prefix + "/teamYlStaffInfo";
    }

    /**
     * 新增团长信息
     */
    @GetMapping("/addTeam")
    public String addTeam(ModelMap mmp) {
        //获取一个团队编号
        String teamCode = teamYlInfoService.selectTeamCode();
        mmp.put("teamCode",teamCode);
        //获取旧团政策信息
        TeamPolicy oldTeamPolicy = new TeamPolicy();
        oldTeamPolicy.setType("1");
        oldTeamPolicy.setTeamType("1");
        List<TeamPolicy> oldYList = teamPolicyService.selectTeamPolicyList(oldTeamPolicy);
        oldTeamPolicy.setType("2");
        oldTeamPolicy.setTeamType("1");
        List<TeamPolicy> oldCList = teamPolicyService.selectTeamPolicyList(oldTeamPolicy);
        oldTeamPolicy.setType("3");
        oldTeamPolicy.setTeamType("1");
        List<TeamPolicy> oldZList = teamPolicyService.selectTeamPolicyList(oldTeamPolicy);

        //获取新团政策信息
       /* TeamPolicy newTeamPolicy = new TeamPolicy();
        newTeamPolicy.setType("1");
        newTeamPolicy.setTeamType("2");
        newTeamPolicy.setRewardType("6");
        List<TeamPolicy> newYList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);
        newTeamPolicy.setType("2");
        newTeamPolicy.setTeamType("2");
        newTeamPolicy.setRewardType("");
        List<TeamPolicy> newCList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);
        newTeamPolicy.setType("3");
        newTeamPolicy.setTeamType("2");
        newTeamPolicy.setRewardType("");
        List<TeamPolicy> newZList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);
        newTeamPolicy.setType("1");
        newTeamPolicy.setRewardType("5");
        newTeamPolicy.setTeamType("2");
        List<TeamPolicy> newGBList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);*/

        mmp.put("oldYList",oldYList);
        mmp.put("oldCList",oldCList);
        mmp.put("oldZList",oldZList);
      /*  mmp.put("newYList",newYList);
        mmp.put("newCList",newCList);
        mmp.put("newZList",newZList);
        mmp.put("newGBList",newGBList);*/
        return prefix + "/addTeam";
    }


    /**
     * 前往新增团员页面
     */
    @GetMapping("/addGroup")
    public String addGroup(ModelMap mmp) {

        return prefix + "/addGroup";
    }


    /**
     * 前往团长查找带回页面
     */
    @GetMapping("/selectTeamView")
    public String selectTeamView()
    {
        return prefix + "/selectTeamView";
    }


    /**
     * 前往获取团长信息页面
     */
    @GetMapping("/selectTeamGroupView")
    public String selectTeamGroupView()
    {
        return prefix + "/selectTeamGroupView";
    }


    /**
     * 前往修改团长密码页面
     */
    @GetMapping("/updatePwd/{agentNum}")
    public String updatePwd(@PathVariable("agentNum") String agentNum, ModelMap mmap){
        if (StringUtils.isEmpty(agentNum)) {
            throw new BusinessException("请选择团长展业端管理编号");
        }
        TeamYlInfoDo teamYlInfoDo = new TeamYlInfoDo();
        teamYlInfoDo.setAgentNum(agentNum);
        TeamYlInfoDo tyl = teamYlInfoService.selectTeamYlInfoList(teamYlInfoDo).get(0);
        mmap.put("tyl",tyl);
        return prefix + "/updatePwd";
    }


    /**
     * 前往选择团员页面
     */
    @GetMapping("/selectAllAgent")
    public String selectAllAgent()
    {
        return prefix + "/selectAllAgent";
    }


    /**
     * 获取 所有的团员信息(展业端管理信息)
     */
    @RequiresPermissions("team:teamYlInfo:list")
    @PostMapping("/selectAllAgentList")
    @ResponseBody
    public TableDataInfo selectAllAgentList(AgentInfo agentInfo) {
        agentInfo.setAgentStatus("0");
        startPage();
        List<AgentInfo> list = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(list);
    }



    /**
     * 查询团员信息列表
     */
    @RequiresPermissions("team:teamYlInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeamYlInfoDo teamYlInfo)
    {
        startPage();
        List<TeamYlInfoDo> list = teamYlInfoService.selectTeamYlInfoList(teamYlInfo);
        return getDataTable(list);
    }

    /**
     * 查询团长信息列表
     */
    @RequiresPermissions("team:team:list")
    @PostMapping("/selectTeamList")
    @ResponseBody
    public TableDataInfo selectTeamList(AgentInfo agentInfo) {
        //获取扶持档位为V9的服务商
        agentInfo.setSupportLevel("V9");
        //扶持等级没有到期的
        agentInfo.setStartDate(DateUtils.getDate());
        //并且状态是正常的
        agentInfo.setAgentStatus("0");
        startPage();
        List<AgentInfo> list = agentInfoService.selectAgentInfoTeamList(agentInfo);
        return getDataTable(list);
    }


    /**
     * 导出团员信息列表
     */
    @RequiresPermissions("team:teamYlInfo:export")
    @Log(title = "团员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeamYlInfoDo teamYlInfo)
    {
        List<TeamYlInfoDo> list = teamYlInfoService.selectTeamYlInfoList(teamYlInfo);
        ExcelUtil<TeamYlInfoDo> util = new ExcelUtil<TeamYlInfoDo>(TeamYlInfoDo.class);
        return util.exportExcel(list, "teamYlInfo");
    }

    /**
     * 新增团员信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存团员信息
     */
    @RequiresPermissions("team:teamYlInfo:add")
    @Log(title = "新增团长", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeamYlInfo teamYlInfo, TeamYl teamYl) {
        return toAjax(teamYlInfoService.insertTeamYlInfo(teamYlInfo,teamYl));
    }


    /**
     * 新增保存团员信息
     */
    @RequiresPermissions("team:teamYlInfo:add")
    @Log(title = "新增团员", businessType = BusinessType.INSERT)
    @PostMapping("/addGroup")
    @ResponseBody
    public AjaxResult addGroup(TeamYlInfo teamYlInfo, TeamYl teamYl) throws ParseException {
        return toAjax(teamYlInfoService.addGroup(teamYlInfo,teamYl));
    }



    /**
     * 修改团长信息
     */
    @GetMapping("/edit/{ylId}")
    public String edit(@PathVariable("ylId") String ylId, ModelMap mmap)
    {
        TeamYlInfoDo teamYlInfo = teamYlInfoService.selectTeamYlInfoById(ylId);
        mmap.put("teamYlInfo", teamYlInfo);
        //获取旧团政策信息
        TeamPolicy oldTeamPolicy = new TeamPolicy();
        oldTeamPolicy.setType("1");
        oldTeamPolicy.setTeamType("1");
        List<TeamPolicy> oldYList = teamPolicyService.selectTeamPolicyList(oldTeamPolicy);
        oldTeamPolicy.setType("2");
        oldTeamPolicy.setTeamType("1");
        List<TeamPolicy> oldCList = teamPolicyService.selectTeamPolicyList(oldTeamPolicy);
        oldTeamPolicy.setType("3");
        oldTeamPolicy.setTeamType("1");
        List<TeamPolicy> oldZList = teamPolicyService.selectTeamPolicyList(oldTeamPolicy);

        //获取新团政策信息
        TeamPolicy newTeamPolicy = new TeamPolicy();
        newTeamPolicy.setType("1");
        newTeamPolicy.setRewardType("6");
        newTeamPolicy.setTeamType("2");
        List<TeamPolicy> newYList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);
        newTeamPolicy.setType("2");
        newTeamPolicy.setTeamType("2");
        List<TeamPolicy> newCList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);
        newTeamPolicy.setType("3");
        newTeamPolicy.setTeamType("2");
        List<TeamPolicy> newZList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);
        newTeamPolicy.setType("1");
        newTeamPolicy.setRewardType("5");
        newTeamPolicy.setTeamType("2");
        List<TeamPolicy> newGBList = teamPolicyService.selectTeamPolicyList(newTeamPolicy);

        mmap.put("oldYList",oldYList);
        mmap.put("oldCList",oldCList);
        mmap.put("oldZList",oldZList);
        mmap.put("newYList",newYList);
        mmap.put("newCList",newCList);
        mmap.put("newZList",newZList);
        mmap.put("newGBList",newGBList);
        return prefix + "/edit";
    }

    /**
     * 修改团员信息
     */
    @GetMapping("/editGroup")
    public String edit(@RequestParam Map params, ModelMap mmap)
    {
        TeamYlInfoDo teamYlInfo = teamYlInfoService.selectTeamYlInfoById(params.get("ylId").toString());
        mmap.put("teamYlInfo", teamYlInfo);
        return prefix + "/editGroup";
    }

    /**
     * 修改保存团员信息
     */
    @RequiresPermissions("team:teamYlInfo:edit")
    @Log(title = "团员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeamYlInfo teamYlInfo,TeamYl teamYl)
    {
        return toAjax(teamYlInfoService.updateTeamYlInfo(teamYlInfo,teamYl));
    }

    /**
     * 修改保存团员信息
     */
    @RequiresPermissions("team:teamYlInfo:edit")
    @Log(title = "修改团员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTeamInfo")
    @ResponseBody
    public AjaxResult updateTeamInfo(TeamYlInfo teamYlInfo) {
        if (StringUtils.isEmpty(teamYlInfo.getPassword())) {
            return error("请输入团长密码");
        }
        teamYlInfo.setPassword(DigestUtils.md5Hex(teamYlInfo.getPassword()));
        return toAjax(teamYlInfoService.updateTeamInfo(teamYlInfo));
    }



    /**
     * 删除团员信息
     */
    @RequiresPermissions("team:teamYlInfo:remove")
    @Log(title = "团员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(teamYlInfoService.deleteTeamYlInfoByIds(ids));
    }

    /**
     * 获取团长信息
     * @param agentNum
     * @return
     */
    @PostMapping("getTeamInfo")
    @ResponseBody
    public AjaxResult getTeamInfo(@RequestBody String agentNum){
        agentNum = agentNum.replace("=","");
        Map map = teamYlInfoService.selectTeamInfoByAgentNum(agentNum);
        return AjaxResult.success(map);
    }


}
